package com.knife.blog.service;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Properties;
import java.util.Random;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Config {

	private static Config singleton = new Config();
	private static String wwwpath;

	public static Config getInstance() {
		return singleton;
	}

	public Config() {
		Properties props = new Properties();
		InputStream in = null;
		try {
			in = getClass().getResourceAsStream("/SysConfig.properties");
			// InputStream in = new BufferedInputStream(new
			// FileInputStream(getClass().getResourceAsStream("/conf.properties")));
			props.load(in);
			wwwpath = props.getProperty("com.knife.blog.wwwpath");
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (Exception e1) {
				}
			}
		}
	}

	public static String getWwwpath() {
		/*
		 * ResourceBundle rb = ResourceBundle.getBundle("SysConfig"); String
		 * wwwpath = rb.getString("com.knife.blog.wwwpath"); if
		 * ((wwwpath.endsWith("/")) || (wwwpath.endsWith("\\"))) wwwpath =
		 * wwwpath.substring(0, wwwpath.length() - 1);
		 */
		return wwwpath;
	}

	public static ResourceBundle getMailconfig() {
		ResourceBundle rb = ResourceBundle.getBundle("SysConfig");
		return rb;
	}

	public static boolean saveMailconfig(String email, String smtp, String ip,
			String needauth, String mailpass) {
		boolean ret = false;
		try {
			String filepath = "";
			java.net.URL classUrl = Config.class
					.getResource("/SysConfig.properties");
			filepath = classUrl.getPath();
			FileInputStream ins = new FileInputStream(filepath);
			Properties prop = new Properties();
			prop.load(ins);
			ins.close();
			if (!mailpass.equals("")) {
				prop.setProperty("com.knife.blog.mailpass", mailpass);
			}
			prop.setProperty("com.knife.blog.email", email);
			prop.setProperty("com.knife.blog.ip", ip);
			prop.setProperty("com.knife.blog.smtp", smtp);
			prop.setProperty("com.knife.blog.needauth", needauth);
			FileOutputStream outs = new FileOutputStream(filepath);
			prop.store(outs, "SysConfig.properties");
			outs.close();
			ret = true;
		} catch (Exception e) {
			System.out.println("邮件配置出错");
			System.out.println(e.getMessage());
			return ret;
		}
		return ret;
	}

	public static void main(String[] args) {
		System.out.println(getWwwpath());
	}

	public static String NoHtml(String msg) {
		String retv = "";
		try {
			Pattern pattern = Pattern.compile("<script[^>]*?>.*?</script>", 2);
			Matcher matcher = pattern.matcher(msg);
			retv = matcher.replaceAll("");
			pattern = Pattern.compile("<(.[^>]*)>", 2);
			retv = pattern.matcher(retv).replaceAll("");
			pattern = Pattern.compile("([\\r\\n])[\\s]+", 2);
			retv = pattern.matcher(retv).replaceAll("");
			pattern = Pattern.compile("-->", 2);
			retv = pattern.matcher(retv).replaceAll("");
			pattern = Pattern.compile("<!--.*", 2);
			retv = pattern.matcher(retv).replaceAll("");
			pattern = Pattern.compile("&(quot|#34);", 2);
			retv = pattern.matcher(retv).replaceAll("\\");
			pattern = Pattern.compile("&(amp|#38);", 2);
			retv = pattern.matcher(retv).replaceAll("&");
			pattern = Pattern.compile("&(lt|#60);", 2);
			retv = pattern.matcher(retv).replaceAll("<");
			pattern = Pattern.compile("&(gt|#62);", 2);
			retv = pattern.matcher(retv).replaceAll(">");
			pattern = Pattern.compile("&(nbsp|#160);", 2);
			retv = pattern.matcher(retv).replaceAll("");

			pattern = Pattern.compile("&#(\\d+);", 2);
			retv = pattern.matcher(retv).replaceAll("");
			retv = retv.replaceAll(" ", "");
			retv = retv.replaceAll("<", "");
			retv = retv.replaceAll(">", "");
			retv = retv.replaceAll("/r", "");
			retv = retv.replaceAll("/n", "");
		} catch (Exception e) {
			retv = msg;
		}
		return retv;
	}

	public static String getValidationCode() {
		String retv = "";
		Random random = new Random();
		for (int i = 0; i < 6; i++) {
			String rand = String.valueOf(random.nextInt(10));
			retv = retv + rand;
		}
		return retv;
	}
}
